tree grammar DecluseChecker;

options {
    tokenVocab=Decluse;                    // import tokens from Calc.tokens
    ASTLabelType=CommonTree;            // AST nodes are of type CommonTree
}

@header {
package vb.week4.decluse;
import java.util.Set;
import java.util.HashSet;
}

// Alter code generation so catch-clauses get replaced with this action. 
// This disables ANTLR error handling: CalcExceptions are propagated upwards.
@rulecatch { 
    catch (RecognitionException e) { 
        throw e; 
    } 
}

@members {
    // idset - a set of declared identifiers.
    private SymbolTable<Entry> symtab = new SymbolTable<Entry>();
}

program
    :   ^(PROGRAM unit*)
    ;
    
unit
    :   ^(DECLARATION id=IDENTIFIER)
        {   if (isDeclared($id.text))
                throw new DeclareException($id, "is already declared");
            else 
                declare($id.getText()); 
        }
    |   ^(USE id=IDENTIFIER)
        {   if (!isDeclared($id.text))
                throw new DeclareException($id, "is not declared");
        }
    ;